package org.jeecg.modules.hc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.hc.entity.HcMainCategories;
import org.jeecg.modules.hc.entity.HcMainRelation;
import org.jeecg.modules.hc.entity.HcSiteAddressDetails;
import org.jeecg.modules.hc.entity.HcSiteRelation;
import org.jeecg.modules.hc.enums.HcUpFlagEnum;
import org.jeecg.modules.hc.service.IHcMainCategoriesService;
import org.jeecg.modules.hc.service.IHcMainRelationService;
import org.jeecg.modules.hc.service.IHcSiteAddressDetailsService;
import org.jeecg.modules.hc.service.IHcSiteRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 主营品类
 * @Author: jeecg-boot
 * @Date: 2022-12-13
 * @Version: V1.0
 */
@Api(tags = "主营品类")
@RestController
@RequestMapping("/hc/hcMainCategories")
@Slf4j
public class HcMainCategoriesController extends JeecgController<HcMainCategories, IHcMainCategoriesService> {
    @Autowired
    private IHcMainCategoriesService hcMainCategoriesService;
    @Autowired
    private IHcSiteAddressDetailsService hcSiteAddressDetailsService;
    @Autowired
    private IHcSiteRelationService hcSiteRelationService;
    @Autowired
    private IHcMainRelationService hcMainRelationService;

    @ApiOperation("获取主营分类列表")
    @GetMapping(value = "/getList")
    public Result<?> getList(@RequestParam(required = false) String addressId) {
        LambdaQueryWrapper<HcMainCategories> lambdaQueryWrapper = new LambdaQueryWrapper<HcMainCategories>()
                .eq(HcMainCategories::getState, HcUpFlagEnum.YES)
                .orderByAsc(HcMainCategories::getSort);

        if (StringUtils.isNotBlank(addressId)) {
            HcSiteAddressDetails hcSiteAddressDetails = hcSiteAddressDetailsService.getById(addressId);

            if (hcSiteAddressDetails != null) {
                String siteId = hcSiteAddressDetails.getSiteId();

                List<HcSiteRelation> hcSiteRelationList = hcSiteRelationService.list(new LambdaQueryWrapper<HcSiteRelation>()
                        .eq(HcSiteRelation::getSiteId, siteId)
                );

                List<String> merchantIds = hcSiteRelationList.stream().map(HcSiteRelation::getMerchantId).collect(Collectors.toList());

                if (!CollectionUtils.isEmpty(merchantIds)) {
                    List<HcMainRelation> hcMainRelationList = hcMainRelationService.list(new LambdaQueryWrapper<HcMainRelation>()
                            .in(HcMainRelation::getMerchantId, merchantIds)
                    );

                    List<String> mainCategoriesIds = hcMainRelationList.stream().map(HcMainRelation::getMainCategoriesId).collect(Collectors.toList());

                    if (!CollectionUtils.isEmpty(mainCategoriesIds)) {
                        lambdaQueryWrapper.in(HcMainCategories::getId, mainCategoriesIds);
                    }
                } else {
                    return Result.ok(null);
                }
            }
        }

        List<HcMainCategories> list = hcMainCategoriesService.list(lambdaQueryWrapper);
        return Result.OK(list);
    }

    /**
     * 分页列表查询
     *
     * @param hcMainCategories
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "主营品类-分页列表查询")
    @ApiOperation(value = "主营品类-分页列表查询", notes = "主营品类-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<HcMainCategories>> queryPageList(HcMainCategories hcMainCategories,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         HttpServletRequest req) {
        QueryWrapper<HcMainCategories> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("SORT");
        queryWrapper.orderByAsc("ID");
        QueryGenerator.installMplus(queryWrapper, hcMainCategories, req.getParameterMap());
        Page<HcMainCategories> page = new Page<HcMainCategories>(pageNo, pageSize);
        IPage<HcMainCategories> pageList = hcMainCategoriesService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param hcMainCategories
     * @return
     */
    @AutoLog(value = "主营品类-添加")
    @ApiOperation(value = "主营品类-添加", notes = "主营品类-添加")
    //@RequiresPermissions("org.jeecg.modules:hc_main_categories:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody HcMainCategories hcMainCategories) {
        hcMainCategoriesService.save(hcMainCategories);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param hcMainCategories
     * @return
     */
    @AutoLog(value = "主营品类-编辑")
    @ApiOperation(value = "主营品类-编辑", notes = "主营品类-编辑")
    //@RequiresPermissions("org.jeecg.modules:hc_main_categories:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody HcMainCategories hcMainCategories) {
        hcMainCategoriesService.updateById(hcMainCategories);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "主营品类-通过id删除")
    @ApiOperation(value = "主营品类-通过id删除", notes = "主营品类-通过id删除")
    //@RequiresPermissions("org.jeecg.modules:hc_main_categories:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        hcMainCategoriesService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
//	@AutoLog(value = "主营品类-批量删除")
//	@ApiOperation(value="主营品类-批量删除", notes="主营品类-批量删除")
    //@RequiresPermissions("org.jeecg.modules:hc_main_categories:deleteBatch")
//	@DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.hcMainCategoriesService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "主营品类-通过id查询")
    @ApiOperation(value = "主营品类-通过id查询", notes = "主营品类-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<HcMainCategories> queryById(@RequestParam(name = "id", required = true) String id) {
        HcMainCategories hcMainCategories = hcMainCategoriesService.getById(id);
        if (hcMainCategories == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(hcMainCategories);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param hcMainCategories
     */
    //@RequiresPermissions("org.jeecg.modules:hc_main_categories:exportXls")
//    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HcMainCategories hcMainCategories) {
        return super.exportXls(request, hcMainCategories, HcMainCategories.class, "主营品类");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("hc_main_categories:importExcel")
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HcMainCategories.class);
    }

}
